<?php
/**
* This file holds a class for Application_Model_BusLineMapper.
* @package busmap
*/

/**
* This class holds Application_Model_BusLineMapper class, that maps BusLine objects to the database entries.
* @package busmap
*/
class Application_Model_BusLineMapper
{
	protected $_dbTable;
 
	/**
	* Function that sets the database table for the BusLine objects.
	*/
	public function setDbTable($dbTable)
	{
		if (is_string($dbTable)) {
			$dbTable = new $dbTable();
		}
		if (!$dbTable instanceof Zend_Db_Table_Abstract) {
			throw new Exception('Invalid table data gateway provided');
		}
		$this->_dbTable = $dbTable;
		return $this;
	}
 
 	/**
	* Function that gets the database table for the BusLine objects.
	*/
	public function getDbTable()
	{
		if (null === $this->_dbTable) {
			$this->setDbTable('Application_Model_DbTable_BusLine');
		}
		return $this->_dbTable;
	}
 
	/**
	* Function that saves one busline to database.
	* If busline id is specified within the object, the corresponding table entry will be updated.
	* Also the busstops that the current busline goes through will be updated (buslineToBusstop entries).
	* If no id has been specified, a new entry will be created.
	*/
	public function save(Application_Model_BusLine $busline)
	{
		$data = array(
			'name'   => $busline->getName(),
			'description' => $busline->getDescription(),
			'delete_time' => $busline->getDeleteTime(),
			'color_id' => $busline->getColorId()
		);
		if (null == ($id = $busline->getId())) {
			$data['add_time'] = time();
			$result = $this->getDbTable()->insert($data);
			return $result;
		} else {
			$this->getDbTable()->update($data, array('id = ?' => $id));
			if(count($busline->busstops)){
				foreach($busline->busstops as $busstops){
					foreach($busstops as $id => $busstop){
						if(count($busstop)){
							$oneBuslineToBusstop = new Application_Model_BusLineToBusStop();
							$oneBuslineToBusstop->setBusLineId($busline->getId())
												->setBusStopId($busstop)
												->setId($id);
							$buslineToBusstop = new Application_Model_BusLineToBusStopMapper();
							$buslineToBusstop->save($oneBuslineToBusstop);
						}
					}
				}				
			}
		}
	}
 
	/**
	* Function that finds a busline entry with the given id and updates the given busline reference.
	* Only buslines with delete_time set to null will be returned.
	*/
	public function find($id, Application_Model_BusLine $busline)
	{
		$result = $this->getDbTable()->find($id);
		if (0 == count($result)) {
			return;
		}
		$row = $result->current();
		if(!$row->delete_time){
			$busline->setId($row->id)
					  ->setName($row->name)
					  ->setDescription($row->description)
					  ->setAddTime($row->add_time)
					  ->setColorId($row->color_id);
		}else{
			return false;
		}
	}
 
	/**
	* Function that returns all buslines that have the delete_time null.
	*/
	public function fetchAll()
	{
		$resultSet = $this->getDbTable()->fetchAll();
		$entries   = array();
		foreach ($resultSet as $row) {
			$entry = new Application_Model_BusLine();
			$entry->setId($row->id)
				  ->setName($row->name)
				  ->setDescription($row->description)
				  ->setAddTime($row->add_time)
				  ->setDeleteTime($row->delete_time)
				  ->setColorId($row->color_id);

			$buslineToBusstop = new Application_Model_BusLineToBusStopMapper();
			$busstops = $buslineToBusstop->findBusStops($entry->id);
			$entry->setBusStops($busstops);

			if(!$entry->deleteTime){
				$entries[] = $entry;
			}
		}
		return $entries;
	}
}